
//启动文件（入口文件）
const Koa = require('koa')
const bodyParser  = require('koa-bodyparser')   //上下文解析
const static = require('koa-static')    //静态资源服务
const path = require('path')
const cors = require('koa-cors')       //跨域访问组件
const registerRouter = require('./routers/index')
const Token = require('./utils/token')
const app = new Koa()
// 配置静态资源文件
const  staticPath = './static'
app.use(static(
    path.join( __dirname, staticPath)
))
app.use(async (ctx,next)=>{
    if (ctx.url.indexOf('/api/users/signIn') == -1){
        let data = Token.decrypt(ctx.header.authorization);
        if (data.token){
            await   next();
        }else {
            ctx.body = {
                code:401,
                message:'failed',
                data:data
            };
        }
    }else{
        await   next();
    }
})
app.use(cors()) // 允许跨域访问
app.use(bodyParser()) // body解析
app.use(registerRouter())
app.listen(3000,() =>{
console.log("服务器已启动，http://localhost:3000");
})
